package com.xyp.mesadmin.md.unit.controller;

import com.github.pagehelper.PageInfo;
import com.xyp.mesadmin.md.unit.param.UnitPageParam;
import com.xyp.mesadmin.md.unit.service.MdUnitMeasureService;
import com.xyp.mesadmin.annotation.MyLog;
import com.xyp.mescommon.common.R;
import com.xyp.mescommon.entity.md.MdUnitMeasure;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * --- 加油，努力 ---
 *
 * @Author XYP
 * @Date 2024/9/24
 * @Solgne 从想做到能做
 * @Desc
 */
@RestController
@CrossOrigin
@RequestMapping("/md/unit")
public class MdUnitMeasureController {
    @Autowired
    private MdUnitMeasureService mdUnitMeasureService;

    //查询所有
    @GetMapping("/unitList")
    public R unitList () {
        List<MdUnitMeasure> mdUnitMeasures = mdUnitMeasureService.unitList();
        return R.ok(mdUnitMeasures);
    }
    //查询
    @GetMapping("/page")
    public R pageUnit (UnitPageParam unitPageParam) {
        PageInfo<MdUnitMeasure> mdUnitMeasurePageInfo = mdUnitMeasureService.findAll(unitPageParam);
        return R.ok(mdUnitMeasurePageInfo);
    }
    //根据id查询查询
    @GetMapping("/unitInfo")
    public R unitInfo (Integer id) {
        MdUnitMeasure mdUnitMeasure = mdUnitMeasureService.findByid(id);
        return R.ok(mdUnitMeasure);
    }
    //删除
    @MyLog("删除计量单位")
    @GetMapping("/del")
    public R del (Integer id) {
        mdUnitMeasureService.deleteUnitById(id);
        return R.ok();
    }
    //批量删除
    @MyLog("批量删除计量单位")
    @GetMapping("/deleteUnitBatch")
    public R deleteUnitBatch (@RequestParam("ids") List<Integer> ids) {
        mdUnitMeasureService.deleteUnitBatch(ids);
        return R.ok();
    }
    //添加
    @MyLog("添加计量单位")
    @PostMapping("/add")
    public R add (@RequestBody MdUnitMeasure mdUnitMeasure) {
        mdUnitMeasureService.addUnit(mdUnitMeasure);
        return R.ok();
    }
    //修改
    @MyLog("修改计量单位")
    @PostMapping("/update")
    public R update (@RequestBody MdUnitMeasure mdUnitMeasure) {
        mdUnitMeasureService.updateUnit(mdUnitMeasure);
        return R.ok();
    }
    //上传文件
    @MyLog("上传文件")
    @PostMapping("/upload/excel")
    public R uploadExcel(MultipartFile file){
        mdUnitMeasureService.importUnit (file);
        return R.ok();
    }
    //导出文件
    @MyLog("导出文件")
    @GetMapping("/download/excel")
    public R downloadExcel (HttpServletResponse response){
        mdUnitMeasureService.exportExcel(response);
        return R.ok();
    }
}
